package com.yenlien.traceyun.control.service;

import com.yenlien.traceyun.control.config.Log;
import com.yenlien.traceyun.control.dao.AccreditCompanyDao;
import com.yenlien.traceyun.control.dao.AccreditDao;
import com.yenlien.traceyun.control.entity.Accredit;
import com.yenlien.traceyun.control.entity.AccreditCompany;
import com.yenlien.traceyun.control.interceptor.UserAuthUtil;
import com.yenlien.traceyun.control.service.rest.FileRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class AccreditService {

    @Autowired
    private FileRestService fileRestService;

    @Autowired
    private AccreditDao accreditDao;

    @Autowired
    private AccreditCompanyDao accreditCompanyDao;

    /**
     * 新增授权管理
     *
     * @param accredit
     * @param files
     */
    @Transactional
    @Log(operation = "授权管理", profession = "新增授权")
    public void save(Accredit accredit, List<MultipartFile> files) {
        List<AccreditCompany> list = accredit.getList();
        String size = String.valueOf(list.size());
        accredit.setCompanyNum(size);
        accredit.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        try {
            fileRestService.upload(null, accredit, files);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Accredit save = accreditDao.save(accredit);
        String id = save.getId();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setAccreditId(id);
        }
        accreditCompanyDao.saveAll(list);
    }

    @Transactional
    @Log(operation = "授权管理", profession = "新增授权")
    public void save2(Accredit accredit) {
        List<AccreditCompany> list = accredit.getList();
        String size = String.valueOf(list.size());
        accredit.setCompanyNum(size);
        accredit.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        Accredit save = accreditDao.save(accredit);
        String id = save.getId();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setAccreditId(id);
        }
        accreditCompanyDao.saveAll(list);
    }

    /**
     * 查询授权
     *
     * @param page
     * @return
     */
    public Page<Accredit> select(Integer page, String companyId) {
        Sort ageSort = Sort.by(Sort.Direction.ASC, "accreditExpire");
        PageRequest pageRequest = PageRequest.of(page, 10, ageSort);
        Page<Accredit> all = accreditDao.findByCompanyId(companyId, pageRequest);
        return all;
    }

    /**
     * 根据名称模糊搜索
     *
     * @param accreditName
     * @return
     */
    @Log(operation = "授权管理", profession = "查询授权")
    public Object search(String accreditName) {
        return accreditDao.findByAccreditNameLike(accreditName);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Log(operation = "授权管理", profession = "查询授权")
    public Object selectId(String id) {
        List<AccreditCompany> byAccreditId = accreditCompanyDao.findByAccreditId(id);
        Accredit byId = accreditDao.findById(id);
        byId.setList(byAccreditId);
        return byId;
    }

    /**
     * 根据id修改
     *
     * @param accredit,specification
     * @return
     */
    @Transactional
    @Log(operation = "授权管理", profession = "修改授权")
    public void update(Accredit accredit, List<MultipartFile> files) {
        List<AccreditCompany> list = accredit.getList();
        String id = accredit.getId();
        if (!files.isEmpty()) {
            String name = files.get(0).getOriginalFilename();
            accredit.setContentImg(name);
            try {
                fileRestService.upload(accreditDao.findById(id), accredit, files);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        accreditDao.save(accredit);
        accreditCompanyDao.saveAll(list);
    }

}
